//
//  BaseViewController.m
//  GK
//
//  Created by Seven on 2018/12/19.
//  Copyright © 2018 Seven. All rights reserved.
//

#import "BaseViewController.h"
#import "HYEdgeInsetsButton.h"
@implementation UIViewController(dealoc)

#pragma mark - Active
- (void)didBecomeActive{
    
}

- (void)willResignActive{
    
}

- (void)viewAppear{
    
}

- (void)viewDisappear{
    
}

#pragma mark - landscape
- (void)willChangeLandscape:(BOOL)isLandscape{
    
}


- (BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}


@end

@interface BaseViewController ()

@end

@implementation BaseViewController
- (void)dealloc{
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _viewShowed = YES;
    self.statusBarStyle = [AppUtils getLightStyle];
    //self.view.backgroundColor = ColorWithHex(0xF8F8F8);
    //self.view.backgroundColor = COLOR_BACKGROUND;
    self.view.backgroundColor = COLOR_WHITE;
    //self.gradientLayer = [self.view gradientColorWithStartPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, 1) fromColor:ColorWithHex(0x1F2538) toColor:ColorWithHex(0x16161A) locations:nil];
    
    self.fd_prefersNavigationBarHidden = YES;
    BaseView *topView = [BaseView initWithFrame:CGRectMake(0, 0, self.view.width, [UIDevice safeAreaTopNavBarHeight])];
    topView.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:topView];
    
    //[topView addLineWithFrame:CGRectMake(0, topView.bottom-0.5, topView.width, 0.5) color:ColorWithHex(0xB6B6B6)];
    self.topView = topView;
    
    UILabel *titleLbl = [UILabel initWithFrame:CGRectMake(60, [UIDevice safeAreaTopHeight], kScreenWidth-120, 44)];
    titleLbl.font = SemiboldFontWithSize(18);
    titleLbl.textColor = COLOR_WHITE;
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLbl];
    //titleLbl.text = _member.name;
    self.titleLbl = titleLbl;
    
    UIButton *backBtn = [UIButton initWithFrame:CGRectMake(0, titleLbl.top, 60, titleLbl.height)];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backBtn setImage:[UIImage templateIconWithName:@"Default_back"]];
    backBtn.tintColor = COLOR_WHITE;
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
    [backBtn addTarget:self action:@selector(onBackAction)];
    [topView addSubview:backBtn];
    self.backBtn = backBtn;
    // Do any additional setup after loading the view.
    //[self setupNavigationBar];
    if (self.title) {
        titleLbl.text = self.title;
    }
    [self updateContentFrame];
}

- (void)setHideTopView:(BOOL)hideTopView{
    _hideTopView = hideTopView;
    self.topView.hidden = hideTopView;
    [self updateContentFrame];
}

- (void)setTopColor:(UIColor *)color{
    self.backBtn.tintColor = color;
    self.titleLbl.textColor = color;
}

- (void)updateContentFrame{
    CGFloat topOffset = 0;
    if (self.hideTopView) {
        topOffset = [UIDevice safeAreaTopHeight];
    }else {
        topOffset = self.topView.bottom;
    }
    CGFloat bottomOffset = 0;
    if (self.tabBarController != nil) {
        bottomOffset = [UIDevice safeAreaTabbarHeight];
    }else {
        bottomOffset = [UIDevice safeAreaBottomHeight];
    }
    self.contentFrame = CGRectMake(0, topOffset, self.view.width, self.view.height-topOffset-bottomOffset);
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = self.statusBarStyle;
    [self.navigationController setNavigationBarHidden:self.fd_prefersNavigationBarHidden animated:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self viewAppear];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    [UIApplication.sharedApplication.keyWindow endEditing:YES];
    
    [self viewDisappear];
}


- (void)onBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}

//navbar
- (void)setupNavigationBar {
    /*
    UINavigationBar *navBar = self.navigationController.navigationBar;
    navBar.tintColor = COLOR_333333;
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName : COLOR_333333}];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:kImageWithColor([UIColor whiteColor]) forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    //navBar.backgroundColor = COLOR_WHITE;
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    //*/
    if (self.parentViewController.childViewControllers.count>1) {
        UIImage *backImage = [[UIImage imageNamed:@"Default_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(leftReturnAction)];
    }
}

- (void)createSubViews {
    
}

//导航栏左边按钮事件
-(void)leftReturnAction
{
    if (self.parentViewController.childViewControllers.count>1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        if (self.presentingViewController) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

#pragma makr - EmptyView

- (void)showEmptyErrorView:(BOOL)isNetworkError{
    if (_ignoreEmptyView) {
        return;
    }
    
    if (isNetworkError) {
        if (_networkErrorEmptyView == nil) {
            _networkErrorEmptyView = [[UIView alloc] initWithFrame:self.view.bounds];
            [_networkErrorEmptyView setBackgroundColor:self.view.backgroundColor];
            
            UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 239, 174)];
            [topImageView setImage:[UIImage imageNamed:@"networkerror"]];
            [_networkErrorEmptyView addSubview:topImageView];
            [topImageView setCenter:CGPointMake(_networkErrorEmptyView.width / 2.0f, _networkErrorEmptyView.height / 2.0f - 54)];
            
            UILabel *noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topImageView.bottom + 23, _networkErrorEmptyView.width, 50)];
            [noticeLabel setFont:PingFangRegularFont(16)];
            [noticeLabel setTextColor:ColorWithHexAplha(0xFFFFFF, 0.5)];
            [noticeLabel setNumberOfLines:0];
            [noticeLabel setTextAlignment:NSTextAlignmentCenter];
            [noticeLabel setText:LocalTitle(@"There is no Internet connection!\nPlease check and try again.")];
            [_networkErrorEmptyView addSubview:noticeLabel];
            
            UIImage *retryImage = [UIImage imageWithColor:COLOR_YELLOW size:CGSizeMake(180, 40)];
            UIButton *retryButton = [UIButton initWithFrame:CGRectMake((_networkErrorEmptyView.width - retryImage.size.width)/2.0f, noticeLabel.bottom + 44, retryImage.size.width, retryImage.size.height)];
            [retryButton setBackgroundImage:retryImage forState:UIControlStateNormal];
            [retryButton setCornerRadius:20];
            [retryButton addTarget:self action:@selector(onEmptyAction) forControlEvents:UIControlEventTouchUpInside];
            [retryButton.titleLabel setFont:PingFangTCMediumFontWithSize(18)];
            [retryButton setTitleColor:COLOR_333333 forState:UIControlStateNormal];
            [retryButton setTitle:LocalTitle(@"Retry") forState:UIControlStateNormal];
            [_networkErrorEmptyView addSubview:retryButton];
        }else {
            _networkErrorEmptyView.frame = self.view.bounds;
        }
        _networkErrorEmptyView.hidden = NO;
        if (_emptyView) {
            [_emptyView setHidden:YES];
        }
        [self.view addSubview:_networkErrorEmptyView];
        [self.view hideLoading];
        if (_topView.superview) {
            [self.view bringSubviewToFront:_topView];
        }
    }else{
        if (_emptyView == nil) {
            _emptyView = [[UIView alloc] initWithFrame:self.view.bounds];
            [_emptyView setBackgroundColor:self.view.backgroundColor];
            
            UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 239, 145)];
            [topImageView setImage:[UIImage imageNamed:@"no_data"]];
            [_emptyView addSubview:topImageView];
            [topImageView setCenter:CGPointMake(_emptyView.width / 2.0f, _emptyView.height / 2.0f - 57.5)];
            
            UILabel *noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topImageView.bottom + 44, _emptyView.width, 50)];
            [noticeLabel setFont:PingFangRegularFont(16)];
            [noticeLabel setTextColor:ColorWithHexAplha(0xFFFFFF, 1)];
            [noticeLabel setNumberOfLines:0];
            [noticeLabel setTextAlignment:NSTextAlignmentCenter];
            [noticeLabel setText:LocalTitle(@"Oops! Something went wrong with your\nInternet service. Please try it again.")];
            [_emptyView addSubview:noticeLabel];
            
            UIImage *retryImage = [UIImage imageWithColor:COLOR_YELLOW size:CGSizeMake(180, 40)];
            UIButton *retryButton = [UIButton initWithFrame:CGRectMake((_emptyView.width - retryImage.size.width)/2.0f, noticeLabel.bottom + 44, retryImage.size.width, retryImage.size.height)];
            [retryButton setBackgroundImage:retryImage forState:UIControlStateNormal];
            [retryButton setCornerRadius:20];
            [retryButton addTarget:self action:@selector(onEmptyAction) forControlEvents:UIControlEventTouchUpInside];
            [retryButton.titleLabel setFont:PingFangTCMediumFontWithSize(18)];
            [retryButton setTitleColor:COLOR_333333 forState:UIControlStateNormal];
            [retryButton setTitle:LocalTitle(@"Retry") forState:UIControlStateNormal];
            [_emptyView addSubview:retryButton];
        }else {
            _emptyView.frame = self.view.bounds;
        }
        _emptyView.hidden = NO;
        if (_networkErrorEmptyView) {
            [_networkErrorEmptyView setHidden:YES];
        }
        [self.view addSubview:_emptyView];
        [self.view hideLoading];
        if (_topView.superview) {
            [self.view bringSubviewToFront:_topView];
        }
    }
    
}

- (void)showEmptyView{
    [self showEmptyView:0];
}
- (void)showEmptyView:(int)statusCode
{
    if (statusCode == 0) {
        [self showEmptyErrorView:YES];
    }else{
        [self showEmptyErrorView:NO];
    }
}

- (void)hideEmptyView{
    if (_emptyView) {
        _emptyView.hidden = YES;
    }
    
    if (_networkErrorEmptyView) {
        _networkErrorEmptyView.hidden = YES;
    }
    
    [self.view hideLoading];
}

- (void)onEmptyAction{
    
}

#pragma mark - view

- (void)didBecomeActive{
    [self viewAppear];
}

- (void)willResignActive{
    [self viewDisappear];
}

- (void)viewAppear{
    _viewShowed = YES;
}

- (void)viewDisappear{
    _viewShowed = NO;
}



#pragma mark - statusBar

- (void)setStatusBarStyle:(NSInteger)statusBarStyle{
    if (_statusBarStyle == statusBarStyle) {
        return;
    }
    _statusBarStyle = statusBarStyle;
}

- (BOOL)prefersStatusBarHidden{
    return NO;//隐藏为YES，显示为NO
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (BOOL)shouldAutorotate{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}


- (void)viewWillLayoutSubviews{
    BOOL isLandscape = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation != UIDeviceOrientationPortrait;
    if (_isLandscape != isLandscape) {
        _isLandscape = isLandscape;
        [self willChangeLandscape:isLandscape];
    }
}

- (void)willChangeLandscape:(BOOL)isLandscape{
    
}

- (void)didUpdateConfig{
    
}

@end
